package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;

public class LoginPanel extends JPanel implements ActionListener {
    private JTextField textname = new JTextField();
    private JTextField textpsd = new JTextField();
    private ATM atm;
    private JButton loginBtn;
    private Icon loginImg = Util.getImageIcon("/images/buttonLand.jpg");
   private int count=3;

    public LoginPanel(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);
//用户名
        JLabel name = new JLabel("用户名:");
        name.setBounds(400, 220, 90, 20);

        name.setForeground(Color.black);//字体颜色
        name.setFont(new Font("宋体", Font.PLAIN, 18));//字体大小
        this.add(name);


        textname.setBounds(500, 220, 100, 20);
        this.add(textname);
//密码
        JLabel psd = new JLabel("密码:");
        psd.setBounds(400, 260, 90, 20);

        psd.setForeground(Color.black);//字体颜色
        psd.setFont(new Font("宋体", Font.PLAIN, 18));//字体大小
        this.add(psd);


        textpsd.setBounds(500, 260, 100, 20);
        this.add(textpsd);


        loginBtn = new JButton(loginImg);
        loginBtn.setBounds(350, 310, 140, 50);
        this.add(loginBtn);
        loginBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginBtn) {
            for (int i = 0; i < atm.getUsers().size(); i++) {
                if (textname.getText().equals(atm.getUsers().get(i).getName() )&&
                        textpsd.getText().equals(atm.getUsers().get(i).getPsd()))
                {
                    atm.setA(i);
                    atm.switchPanel("menu");
                    return;
                }

            }
           JOptionPane.showMessageDialog(this,"输入有误，请重新输入");
            count--;
            if (count==0){
                JOptionPane.showMessageDialog(this,"多次错误，强制退出");
                System.exit(0);
            }
//
        }
    }
}